package com.cloudcc.mobile.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MenuToggleEvent;
import com.cloudcc.cloudframe.util.SharePreferece;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.AppManager;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dialog.CustomProgressDialog;
import com.cloudcc.mobile.dialog.HelpDialog;
import com.cloudcc.mobile.manager.CloudCCPushManager;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.service.BindingService;
import com.cloudcc.mobile.service.DingWeiService;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.umeng.message.IUmengCallback;

public class DialogUtils {
	private static String mEn = RunTimeManager.getInstance().getlanguage();
	public static int a = 0, b = 0, c = 0, d = 0, e = 0;
	public CustomProgressDialog progressDialog;
	private Context context;
	public OnDoClickListener onItemListener;

	public void setOnDoClicktListener(OnDoClickListener onItemListener) {
		this.onItemListener = onItemListener;
	}

	/**
	 * @deprecated 乐视项目的时候开始取消 第一次登录指导页
	 */
	public static void isFirstDialog(final Context context) {
		if (UserManager.getManager().isFrist()) {
			try {
				final HelpDialog help = new HelpDialog(context);
				final Dialog rateDialog = help.createHelpHome();
				help.homeClick(new OnClickListener() {
					@Override
					public void onClick(View v) {
						DialogUtils.a++;
						rateDialog.dismiss();
						final Dialog MessageDialog = help.createHelpMessage();
						help.MessageClick(new OnClickListener() {
							@Override
							public void onClick(View v) {
								DialogUtils.b++;
								MessageDialog.dismiss();
								final Dialog addDialog = help.createHelpAdd();
								help.AddClick(new OnClickListener() {
									@Override
									public void onClick(View v) {
										DialogUtils.c++;
										addDialog.dismiss();
										final Dialog menuDialog = help
												.createHelpMenu();
										help.menuClick(new OnClickListener() {
											@Override
											public void onClick(View v) {
												DialogUtils.d++;
												menuDialog.dismiss();
												EventEngine
														.post(new MenuToggleEvent(
																false, false));
												final Dialog topDialog = help
														.createHelpTop();
												help.TopNoFirstClick(new OnClickListener() {
													@Override
													public void onClick(View v) {
														DialogUtils.e++;
														topDialog.dismiss();
														DialogUtils
																.setNoFirst(context);
													}
												});
												help.TopClick(new OnClickListener() {
													@Override
													public void onClick(View v) {
														topDialog.dismiss();
													}
												});
												if (DialogUtils.e == 0) {
													topDialog.show();
												}
											}
										});
										if (DialogUtils.d == 0) {
											menuDialog.show();
										}
									}
								});
								if (DialogUtils.c == 0) {
									addDialog.show();
								}
							}
						});
						if (DialogUtils.b == 0) {
							MessageDialog.show();
						}
					}
				});
				if (DialogUtils.a == 0) {
					rateDialog.show();
				}
			} catch (Exception e) {
				Tools.handle(e);
			}
		}
	}

	public interface OnDoClickListener {
		public void confirm();

		public void cancel();
	}

	public DialogUtils(Context context) {
		this.context = context;
		progressDialog = new CustomProgressDialog(context);
	}

	/**
	 * 得到自定义的progressDialog
	 * 
	 * @param context
	 * @param msg
	 * @return
	 */
	public static Dialog createLoadingDialog(Context context, String msg) {

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.login_progress_dialog, null);// 得到加载view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
		// main.xml中的ImageView
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
		// 加载动画
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
				context, R.anim.loading_animation);
		// 使用ImageView显示动画
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		tipTextView.setText(msg);// 设置加载信息

		Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog

		loadingDialog.setCancelable(false);// 不可以用“返回键”取消
		loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
		return loadingDialog;

	}

	/**
	 * 退出dialog
	 * 
	 * @param context
	 */
	public static void showExit(final Context context) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		View view = LayoutInflater.from(context).inflate(R.layout.getmessage,
				null);
		final CheckBox getMessage = (CheckBox) view
				.findViewById(R.id.getmessage);
		builder.setView(view);
		// builder.setMessage("亲！确认退出吗？");
		String tuichu ;
		String houtai;
		String quxioa;
		String queren;
		if ("en".equals(mEn)) {
			 tuichu = "Exit the program";
			 houtai = "Background process";
			 quxioa = "Cancel";
			 queren = "Confirm";
		}else{
			 tuichu = "退出程序";
			 houtai = "后台运行";
			 quxioa = "取消";
			 queren = "确认";
		}
			builder.setTitle(tuichu);
			builder.setNeutralButton(houtai, new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(context, MainUIActivity.class);
					AppContext.getInstance().showNotification(intent);
					Intent i = new Intent(Intent.ACTION_MAIN);
					i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					i.addCategory(Intent.CATEGORY_HOME);
					context.startActivity(i);
				}
			});
			builder.setNegativeButton(queren, new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					/**
					 * 清除通知
					 */
					NotificationUtils.clearNotification(context, 0);
					NotificationUtils.clearNotification(context, 1);
					NotificationUtils.clearNotification(context, 2);
					NotificationUtils.clearNotification(context, 3);
					/**
					 * 关闭定位上传
					 */
					AppContext.isStopLocation = true;
					Intent service = new Intent(context, DingWeiService.class);
					context.stopService(service);
					/**
					 * 关闭service获取binding
					 */
					AppContext.isStopService = true;
					Intent binding = new Intent(context, BindingService.class);
					context.stopService(binding);
					/**
					 * 是否接收离线消息
					 */
					if (!getMessage.isChecked()) {
						SharePreferece share = new SharePreferece(context);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("name", UserManager.getManager().getUser().userName);
						map.put("password", "");
						map.put("rememberMima", true);
						map.put("loginAuto", false);
						share.SaveShare("user", map);
						CloudCCPushManager.getInstance().stopPushWork();
					} else {
						CloudCCPushManager.getInstance().resumePushWork();
						/**
						 * 因为是要接受，所以要吧密码账户保存以便自动登陆
						 */
						try {
							SharePreferece share = new SharePreferece(context);
							// String
							// jmPassword=AESUtils.encrypt(Tools.getpassword(),UserManager.getManager().getUser().password);
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("name",
									UserManager.getManager().getUser().email);
							// map.put("password", jmPassword);
							map.put("rememberMima", true);
							map.put("loginAuto", true);
							share.SaveShare("user", map);
						} catch (Exception e) {
							Tools.handle(e);
						}
						AppContext.islogining = false;// 吧登陆状态设为不登陆状态
						AppContext.isFenXiang = false;// 设为不能分享
					}
					AppManager.getInstance().AppExit();
				}
			});
			builder.setPositiveButton(quxioa, null);
			builder.show();
		
		
	}

	/**
	 * 清除缓存dialog
	 * 
	 * @param context
	 */
	public static void showClearCache(final Context context) {
		/**
		 * 系统默认的dialog
		 */
		// AlertDialog.Builder builder=new AlertDialog.Builder(context);
		// builder.setMessage("亲！确认清除吗？");
		// builder.setTitle("清除缓存");
		// builder.setNegativeButton("确认", new AlertDialog.OnClickListener() {
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// DataClearManager.cleanAll(context);
		// File file=new File(Tools.getImagePath("/CloudCC/"));
		// FileUtil.RecursionDeleteFile(file);
		// Tools.showInfo(context, "清除完毕");
		// }
		// });
		// builder.setPositiveButton("取消", null);
		// builder.show();

		/**
		 * 自定义的dialog
		 */
		final AlertDialog builder = new AlertDialog.Builder(context).create();
		View view = LayoutInflater.from(context)
				.inflate(R.layout.zhuxiao, null);
		builder.setView(view);
		builder.show();
		Button queren = (Button) view.findViewById(R.id.queren);
		Button quxiao = (Button) view.findViewById(R.id.quxiao);
		TextView titleName = (TextView) view.findViewById(R.id.titlename);
		TextView bodyName = (TextView) view.findViewById(R.id.bodyname);
		if ("en".equals(mEn)) {
			titleName.setText("Clear cache");
			bodyName.setText("Dear! Are you sure to clear it?？");
		}else{
			titleName.setText("清除缓存");
			bodyName.setText("亲！确认清除吗？");
		}
		
		queren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DataClearManager.cleanAll(context);
				File file = new File(Tools.getImagePath("/CloudCC/"));
				FileUtil.RecursionDeleteFile(file);
				builder.cancel();
				if ("en".equals(mEn)) {
					Tools.showInfo(context, "Cleared finished");
				}else{
					Tools.showInfo(context, "清除完毕");
				}
				
			}
		});
		quxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.cancel();
			}
		});
	}

	/**
	 * 公用dialog
	 * 
	 * @param context
	 */
	public void showDialog(final Context context, String title, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message);
		builder.setTitle(title);
		if ("en".equals(mEn)) {
			builder.setNegativeButton("Confirm", new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					onItemListener.confirm();
				}
			});
			builder.setPositiveButton("Cancel", null);
		}else{
			builder.setNegativeButton("确认", new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					onItemListener.confirm();
				}
			});
			builder.setPositiveButton("取消", null);
		}
		builder.show();

	}

	/**
	 * 更新自定义dialog
	 * 
	 * @param context
	 */
	public void showUpdateDialog(final Context context, String title,
			String message) {
		/**
		 * 自定义的dialog
		 */
		final AlertDialog builder = new AlertDialog.Builder(context).create();
		builder.setCancelable(false);
		View view = LayoutInflater.from(context)
				.inflate(R.layout.zhuxiao, null);
		builder.setView(view);
		builder.show();
		Button queren = (Button) view.findViewById(R.id.queren);
		Button quxiao = (Button) view.findViewById(R.id.quxiao);
		TextView titleName = (TextView) view.findViewById(R.id.titlename);
		TextView bodyName = (TextView) view.findViewById(R.id.bodyname);
		titleName.setText(title);
		bodyName.setText(message);
		queren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.confirm();
				builder.cancel();
			}
		});
		quxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.cancel();
				onItemListener.cancel();
			}
		});
	}

	/**
	 * 下载全能名片王
	 */
	public void showCamDialog(final Context context, String message) {
		/** * 自定义的dialog */
		final AlertDialog builder = new AlertDialog.Builder(context).create();
		View view = LayoutInflater.from(context).inflate(R.layout.install_cam,
				null);
		builder.setView(view);
		builder.show();
		Button download = (Button) view.findViewById(R.id.download);
		Button cancle = (Button) view.findViewById(R.id.cancle);
		TextView bodyName = (TextView) view.findViewById(R.id.t);
		bodyName.setText(message);
		download.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.confirm();
				builder.cancel();
			}
		});
		cancle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.cancel();
				onItemListener.cancel();
			}
		});
	}

	/**
	 * 更新提醒dialog
	 * 
	 * @param context
	 */
	public void showUpdateAppDialog(final Context context, String title,
			String message) {
		/**
		 * 自定义的dialog
		 */
		final AlertDialog builder = new AlertDialog.Builder(context).create();
		View view = LayoutInflater.from(context).inflate(R.layout.update_app,
				null);
		builder.setView(view);
		builder.show();
		Button queren = (Button) view.findViewById(R.id.queren);
		Button quxiao = (Button) view.findViewById(R.id.quxiao);
		TextView titleName = (TextView) view.findViewById(R.id.titlename);
		TextView bodyName = (TextView) view.findViewById(R.id.bodyname);
		titleName.setText(title);
		bodyName.setText(message);
		queren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.confirm();
				builder.cancel();
			}
		});
		quxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.cancel();
				onItemListener.cancel();
			}
		});
	}

	/**
	 * dialog开始显示
	 */
	public void startProgressDialog(Context context) {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(context);
			progressDialog.setCancelable(true);
			progressDialog.setCanceledOnTouchOutside(false);
			if ("en".equals(mEn)) {
				progressDialog.setMessage("Is loading...");
			}else{
				progressDialog.setMessage("正在加载中...");
			}
		
		}
		progressDialog.show();
	}

	/**
	 * 停止dialog
	 */
	public void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	public static boolean isFirst(Context context) {
		SharePreferece share = new SharePreferece(context);
		Map<String, ?> map = share.getShare("firstlogin");
		if (map == null || map.isEmpty()) {
			return true;
		} else {
			boolean isfisrt = (Boolean) map.get("first");
			if (isfisrt) {
				return true;
			} else {
				return false;
			}
		}
	}

	public static void setNoFirst(Context context) {
		SharePreferece share = new SharePreferece(context);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("first", false);
		share.SaveShare("firstlogin", map);
	}

	/**
	 * 自定义退出dialog
	 */
	public static void Exit(final Context context) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.cloudcc_finish, null);
		final AlertDialog builder = new AlertDialog.Builder(context).create();
		builder.setView(view);
		builder.show();
		Button quxiao = (Button) view.findViewById(R.id.quxiao_finish);
		Button queren = (Button) view.findViewById(R.id.queren_finish);
		Button houtai = (Button) view.findViewById(R.id.houtai);
		final CheckBox box = (CheckBox) view.findViewById(R.id.jieshouxiaoxi);
		quxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.cancel();
			}
		});
		queren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				/**
				 * 是否接收离线消息
				 */
				if (!box.isChecked()) {
					CloudCCPushManager.getInstance().stopPushWork();
				} else {
					CloudCCPushManager.getInstance().resumePushWork();
					AppContext.islogining = false;// 吧登陆状态设为不登陆状态
					AppContext.isFenXiang = false;// 设为不能分享
				}
				AppManager.getInstance().AppExitOld();
				AppManager.getInstance().AppExit();
			}
		});
		houtai.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, MainUIActivity.class);
				AppContext.getInstance().showNotification(intent);
				Intent i = new Intent(Intent.ACTION_MAIN);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				i.addCategory(Intent.CATEGORY_HOME);
				context.startActivity(i);
				builder.cancel();
			}
		});
	}
}
